
import axios from 'axios'
import { ElMessage } from 'element-plus';


let baseUrl = "http://47.103.119.27:8011";
// let baseUrl = "http://192.168.20.62:5198";
if (import.meta.env.MODE !== "development") {
  baseUrl = "http://47.103.119.27:8011";
  // baseUrl = "http://192.168.20.62:5198";
}

export const ApiBase=baseUrl;
/** 需要登录 */
export function requestPost(config: any) {
  //创建实例
  const instance = axios.create({
    baseURL: baseUrl,
    timeout: 1000 * 60,
    method: "post"
  })
  //拦截器
  instance.interceptors.request.use(config => {
    let token = localStorage.getItem("token") || "";
    config.headers.token = token;
    config.headers.contentType = "application/json";
    return config;
  }, err => {
    console.log(err)
  });


  instance.interceptors.response.use(res => {

    let data = res.data;

    if (data.success === false) {
      if (data.code === "000008") {
        //token 无效
        window.localStorage.clear();

        ElMessage.error("登录过期")
      }
    }
    return res.data;
  }, err => {
    console.log(err)
  });

  return instance(config);
}

/**不需要登录 */
export function requestPostNoLogin(config: any) {
  //创建实例
  const instance = axios.create({
    baseURL: baseUrl,
    timeout: 1000 * 60,
    method: "post"
  })
  //拦截器
  instance.interceptors.request.use(config => {
    config.headers.contentType = "application/json";
    return config;
  }, err => {
    console.log(err)
  });

  instance.interceptors.response.use(res => {

    let data = res.data;

    if (data.success === false) {
      if (data.code === "000008") {
        //token 无效
        window.localStorage.clear();

        ElMessage.error("登录过期")
      }
    }
    return data;
  }, err => {
    console.log(err)
  });
  return instance(config);
}
